// pages/land/changepsd/changepsd.js
var app = getApp()
var maxTime = 60
var interval = null
var currentTime = -1 //倒计时的事件（单位：s）  
var server = require('../../../utils/server');
Page({
  data: {
    login: false,
    time: '获取验证码',
    countDownNum:60,
    mobile: '',
    num: '',
    num1: '',
    pass: '',
    remindpass: '',
    auth_key: ''
  },
  onLoad: function (options) {
    currentTime = -1;
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ height: res.windowHeight })
      }
    })
  },
  //倒计时
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;
    that.setData({
      timer: setInterval(function () {
        countDownNum--;
        that.setData({
          time: countDownNum +'s'
        })
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          that.setData({
            time: '获取验证码'
          })
        }
      }, 1000)
    })
  },
  //手机号
  getPhoneNum: function (e) {
    this.setData({
      mobile: e.detail.value,
    });
  },
  //获取验证码
  getnum: function (e) {
    var that = this;
    let _params = {
      mobile: that.data.mobile,
      type: 1
    }
    if (_params.mobile.length == 11) {
    //   server.postJSON('/user/sendsms', _params, (res) => {
    //     console.log(res)
    //     that.countDown()
    //     that.setData({
    //         num1: res.data.list.code,
    //         auth_key: res.data.list.auth_key
    //     });
    //     wx.showToast({
    //       title: res.data.msg,
    //       icon: 'none'
    //     });
    //   })
        server.send('/user/sendsms', 'POST', _params).then(res => {
            that.countDown()
            console.log(res)
            that.setData({
                num1: res.list.code,
                auth_key: res.list.auth_key
            });
            wx.showToast({
                title: res.msg,
                icon: 'none'
            });
        })
    }
    else {
      wx.showToast({
        title: '手机号格式错误！',
        icon: 'none'
      })
    }

  },
  //验证码
  inputNum: function (e) {
    // this.data.num = e.detail.value;
    this.setData({
      num: e.detail.value,
    });
  },
  //密码
  inputPass: function (e) {
    this.setData({
      pass: e.detail.value,
    });
  },
  //确认密码
  inputRemindPass: function (e) {
    this.setData({
      remindpass: e.detail.value,
    });
  },
  //完成
  quick_register_phone: function (e) {
    let that = this
    let num = that.data.num
    let num1 = that.data.num1
    let pass = that.data.pass
    let remindpass = that.data.remindpass
    let auth_key = that.data.auth_key
    let _params = {
            mobile: that.data.mobile,
            password: pass,
            password1: remindpass,
            auth_key: auth_key
    }
    console.log(auth_key)
    if (num == num1) {
        if (pass == remindpass) {
            // server.postJSON('/user/newpassword', _params, (res) => {
            //     wx.showToast({
            //       title: res.data.msg,
            //       icon: 'none'
            //     })
            // })
            server.send('/user/newpassword', 'POST', _params).then(res => {
              console.log(res)
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            })
          } else {
                wx.showToast({
                  title: '两次密码不一致！',
                  icon: 'none'
                })
        }
    } else {
        wx.showToast({
          title: '验证码错误！',
          icon: 'none'
        })
    }

  },
})